/*
  *  商城配置:商户通过列表
 */
$(function () {
    $("#jqGrid").Grid({
        url: '../merchant/list',
        colModel: [
			{label: 'id', name: 'id', index: 'id', key: true, hidden: true},
            {label: '商户编号', name: 'id', index: 'id', width: 50},
            {label: '平台用户编号', name: 'userId', index: 'user_id', width: 50},
            {label: '平台用户手机', name: 'ddPhone', index: 'dd_phone', width: 80},
            {label: '商户姓名', name: 'realname', index: 'realname', width: 50},
            {label: '商户名称', name: 'name', index: 'name', width: 50},
            {label: '商户手机号', name: 'phone', index: 'phone', width: 50},
            // {label: '商户地址', name: 'address', index: 'address', width: 50},
            {label: '法人身份证号', name: 'idcard', index: 'idcard', width: 80},
            // {label: '身份证正面', name: 'value', index: 'value', width: 50,
            //     formatter:function (value) {
            //         return transImg(value);
            //     }
            // },
            // {label: '身份证反面', name: 'value1', index: 'value1', width: 50,
            //     formatter:function (value) {
            //         return transImg(value);
            //     }
            // },
            {label: '商户资质', name: 'license', index: 'license', width: 50,
                formatter:function (value) {
                    return transImg(value);
                }
            },
            // {label: '售卖商品简述', name: 'institution', index: 'institution', width: 80},
            // {label: '开户银行', name: 'value2', index: 'value2', width: 80},
            // {label: '开户银行城市', name: 'value5', index: 'value5', width: 80},
            // {label: '开户支行', name: 'value3', index: 'value3', width: 80},
            // {label: '银行账户', name: 'value4', index: 'value4', width: 80},
			{label: '状态', name: 'statu', index: 'statu', width: 30,formatter:function(value){
			    if(value==1){
			        return "启用";
                }else{
                    return "关闭";
                }
                }}
			// {label: '创建时间', name: 'createDate', index: 'create_date', width: 50}
        ]
    });
});

let vm = new Vue({
	el: '#rrapp',
	data: {
        showList: true,
        title: null,
		merchant: {
            value1:'',
            value2:'',
            value3:'',
            value4:'',
            value5:''
        },
		ruleValidate: {
			name: [
				{required: true, message: '名称不能为空', trigger: 'blur'}
			]
		},
		q: {
            userId: '',
            userPhone: '',
            userName: ''
		}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function () {
			vm.showList = false;
			vm.title = "新增";
			vm.merchant = {};
		},
		update: function (event) {
            let id = getSelectedRow("#jqGrid");
			if (id == null) {
				return;
			}
			vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
            let url = vm.merchant.id == null ? "../merchant/saveObject" : "../merchant/updateObject";
            Ajax.request({
			    url: url,
                params: JSON.stringify(vm.merchant),
                type: "POST",
			    contentType: "application/json",
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
			});
		},
		del: function (event) {
            let ids = getSelectedRows("#jqGrid");
			if (ids == null){
				return;
			}

			confirm('确定要删除选中的记录？', function () {
                Ajax.request({
				    url: "../merchant/delete",
                    params: JSON.stringify(ids),
                    type: "POST",
				    contentType: "application/json",
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
					}
				});
			});
		},
		getInfo: function(id){
            Ajax.request({
                url: "../merchant/info/"+id,
                async: true,
                successCallback: function (r) {
                    vm.merchant = r.merchant;
                }
            });
		},
		reload: function (event) {
			vm.showList = true;
            let page = $("#jqGrid").jqGrid('getGridParam', 'page');
			$("#jqGrid").jqGrid('setGridParam', {
                postData: {
                    'userId': vm.q.userId,
                    'userPhone': vm.q.userPhone,
                    'userName': vm.q.userName
                },
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
		},
        reloadSearch: function() {
            vm.q = {
                userId: '',
                userPhone: '',
                userName: ''
            }
            vm.reload();
        },handleSuccessLicense: function (res, file) {
            vm.merchant.license = file.response.url;
        },eyeImageLicense: function () {
            var url = vm.merchant.license;
            eyeImage(url);
        },handleSuccessLogo: function (res, file) {
            vm.merchant.logo = file.response.url;
        },eyeImageLogo: function () {
            var url = vm.merchant.logo;
            eyeImage(url);
        },handleSuccessIdUp: function (res, file) {
            vm.merchant.value = file.response.url;
        }, eyeImageIdUp: function () {
            var url = vm.merchant.value;
            eyeImage(url);
        },handleSuccessIdDown: function (res, file) {
            vm.merchant.value1 = file.response.url;
        },eyeImageIdDown: function () {
            var url = vm.merchant.value1;
            eyeImage(url);
        },
        eyeImage: function (e) {
            eyeImage($(e.target).attr('src'));
        },
        handleFormatError: function (file) {
            this.$Notice.warning({
                title: '文件格式不正确',
                desc: '文件 ' + file.name + ' 格式不正确，请上传 jpg 或 png 格式的图片。'
            });
        },
        handleMaxSize: function (file) {
            this.$Notice.warning({
                title: '超出文件大小限制',
                desc: '文件 ' + file.name + ' 太大，不能超过 2M。'
            });
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        },handleSuccessOpenLicence: function (res, file) {
            vm.merchant.openLicence = file.response.url;
        },
        eyeImageOpenLicence: function () {
            var url = vm.merchant.openLicence;
            eyeImage(url);
        }
	}
});